//
//  OrdersView.swift
//  JiaoZiTaxi
//
//  Created by Hu Gang on 2022/12/7.
//

import SwiftUI

struct OrdersView: View {
    @Environment(\.dismiss) var dismiss
//    @FetchRequest(entity: TaxiOrder.entity(), sortDescriptors: [
//        NSSortDescriptor(keyPath:\TaxiOrder.startTime,ascending:false)
//    ]) var orders : FetchedResults<TaxiOrder>
    @State var orders : [OrderModel]  = []
    var body: some View {
        
        NavigationView {
            VStack {
                        List(orders,id:\.id) { order in
                            OrderItemView(order:order)
                            .background(.white)
                            .listRowBackground(Color.clear) // 清除默认背景
                            .listRowSeparatorTint(.clear)
                            .listRowInsets(EdgeInsets(top: 7.5, leading: 0, bottom: 7.5, trailing: 0))
                            .cornerRadius(16)
                        }
                    }
        } .navigationBarBackButtonHidden(true) // 隐藏默认返回按钮
            .navigationBarItems(leading: backButton) // 添加自定义返回按钮
            .navigationTitle("我的订单")
            .onAppear(){
                Api.getOrderList(openId: ViewModel.shared.passenger.openId) { r in
                    
                    switch r {
                    case .failure(let err):
                        print("[Voice] login error:\(err.localizedDescription)")
                        
                    case .success(let rr):
                        
                        DispatchQueue.main.async {
                            self.orders = rr
                        }
                    }
                }
            }
    }
    var backButton: some View {
        Button(action: {
            dismiss()
        }) {
            HStack {
                Image(systemName: "chevron.backward").foregroundColor(Color(hex: 0x333333))
            }
        }
    }
}

struct OrdersView_Previews: PreviewProvider {
    static var previews: some View {
        OrdersView()
    }
}
